<template>
  <div class="net_detail_table_container">
    <!-- 表格 -->
    <el-tabs v-model="activeName" class="demo-tabs" @tab-change="onTabChange">
      <!-- 工单明细 -->
      <el-tab-pane label="工单明细" name="first">
        <div class="table">
          <el-table :border="true" :data="OrderList" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <!-- 序号 -->
            <el-table-column fixed="left" label="序号" width="50" type="index" :align="'center'" />

            <!-- 工单编号 -->
            <el-table-column label="工单编号" prop="workNo" show-overflow-tooltip width="160" />

            <!-- 工单费用 -->
            <el-table-column label="工单费用" prop="totalPrice" show-overflow-tooltip width="80">
              <template #default="scope">￥{{ (scope.row.totalPrice || 0).toFixed(2) }}</template>
            </el-table-column>

            <!-- 公司 -->
            <el-table-column label="公司" prop="userName" show-overflow-tooltip width="100" />

            <!-- 公司电话 -->
            <el-table-column label="公司电话" prop="userPhone" show-overflow-tooltip width="108" />

            <!-- 客服 -->
            <el-table-column label="客服" prop="emp" show-overflow-tooltip width="80" />

            <!-- 申请时间 -->
            <el-table-column label="申请时间" prop="createTime" show-overflow-tooltip width="150" />

            <!-- 更新时间 -->
            <el-table-column label="更新时间" prop="updateTime" show-overflow-tooltip width="150" />

            <!-- 结算状态 -->
            <el-table-column
              label="结算状态"
              prop="settlement"
              width="80"
              :align="'center'"
              fixed="right"
            >
              <template #default="i">
                <div class="flex_justify_center">
                  <h-status v-if="i.row.settlement">已结算</h-status>
                  <h-status v-else type="info">待结算</h-status>
                </div>
              </template>
            </el-table-column>

            <!-- 状态 -->
            <el-table-column label="状态" prop="status" width="120" :align="'center'" fixed="right">
              <template #default="scope">
                <div class="flex_justify_center">
                  <!-- <el-tag class="ml-2" type="warning" v-if="scope.row.status == 1">待客服确认</el-tag> -->
                  <HStatus v-if="scope.row.status === 1" type="warning">待派单</HStatus>
                  <HStatus v-if="scope.row.status === 2" type="warning">待网点确认</HStatus>
                  <HStatus v-if="scope.row.status === 3" type="warning">处理中</HStatus>
                  <HStatus v-if="scope.row.status === 4" type="success">已完成</HStatus>
                  <HStatus v-if="scope.row.status === 5" type="info">已取消</HStatus>
                </div>
              </template>
            </el-table-column>

            <!-- 操作 -->
            <el-table-column fixed="right" :align="'center'" label="操作" width="60">
              <template #default="scope">
                <HLink @click="goWork(scope.row.id)">详细</HLink>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>

      <!-- 工单物料 -->
      <el-tab-pane label="工单物料" name="second">
        <div class="table">
          <el-table :border="true" :data="OrderList" resizable stripe height="100%">
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <!-- 序号 -->
            <el-table-column
              fixed="left"
              label="序号"
              width="50"
              show-overflow-tooltip
              type="index"
              :align="'center'"
            />

            <!-- 物料单号 -->
            <el-table-column
              fixed="left"
              label="物料单号"
              width="150"
              prop="code"
              show-overflow-tooltip
            >
              <!-- <template #default="scope">
                  <span class="number" @click="viewDetail(scope.row.id)">{{ scope.row.code }}</span>
              </template> -->
            </el-table-column>

            <!-- 工单单号 -->
            <el-table-column label="工单单号" prop="workNo" show-overflow-tooltip width="160">
              <!-- <template #default="scope">
                  <span class="number" @click="goOrder(scope.row.workId)">{{ scope.row.workNo
                  }}</span>
              </template> -->
            </el-table-column>

            <!-- 状态 -->
            <el-table-column label="状态" prop="state" width="83" :align="'center'" fixed="right">
              <template #default="scope">
                <div class="flex_justify_center">
                  <HStatus v-if="scope.row.status === 1" type="warning">待审核</HStatus>
                  <HStatus v-if="scope.row.status === 2" type="success">已通过</HStatus>
                  <HStatus v-if="scope.row.status === 3" type="error">已驳回</HStatus>
                  <HStatus v-if="scope.row.status === 4" type="info">已撒回</HStatus>
                  <HStatus v-if="scope.row.status === 5" type="info">已取消</HStatus>
                </div>
              </template>
            </el-table-column>

            <!-- 申请类型 -->
            <el-table-column label="申请类型" prop="type" show-overflow-tooltip width="100" />

            <!-- 物料名称 -->
            <el-table-column label="物料名称" prop="model" show-overflow-tooltip width="200" />

            <!-- 就见处理方式 -->
            <el-table-column label="旧件处理方式" show-overflow-tooltip width="100">
              <template #default="i">
                {{ i.row.oldCopeWay == 1 ? '废弃' : i.row.oldCopeWay == 2 ? '旧件退回' : '' }}
              </template>
            </el-table-column>

            <!-- 支付网点总费用 -->
            <el-table-column
              label="支付网点总费用"
              prop="networkUndertakePrice"
              show-overflow-tooltip
              width="150"
            >
              <template #default="i">￥{{ i.row.networkUndertakePrice }}</template>
            </el-table-column>

            <!-- 申请人员 -->
            <el-table-column label="申请人员" prop="applyEmp" show-overflow-tooltip width="120" />

            <!-- 审核人员 -->
            <el-table-column label="审核人员" prop="reviewEmp" show-overflow-tooltip width="120" />
            <!-- <el-table-column show-overflow-tooltip prop="network" label="申请网点" width="150" /> -->

            <!-- 申请时间 -->
            <el-table-column label="申请时间" prop="createTime" show-overflow-tooltip width="130" />

            <!-- 审核时间 -->
            <el-table-column label="审核时间" prop="reviewTime" show-overflow-tooltip width="130" />
            <!-- <el-table-column fixed="right" label="操作" width="60">
                <template #default="scope">
                    <span class="waiting_x" v-if="scope.row.status == 1"
                        @click="viewCheck(scope.row)">审核</span>
                    <span class="waiting_pass" v-else>审核</span>
                </template>
            </el-table-column> -->
          </el-table>
        </div>
      </el-tab-pane>

      <!-- 工单费用 -->
      <el-tab-pane label="工单费用" name="third">
        <div class="table">
          <el-table :border="true" :data="OrderList" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <!-- 序号 -->
            <el-table-column
              fixed="left"
              label="序号"
              width="50"
              show-overflow-tooltip
              type="index"
              :align="'center'"
            />

            <!-- 费用单号 -->
            <el-table-column
              fixed="left"
              label="费用单号"
              width="150"
              prop="feeNo"
              show-overflow-tooltip
            >
              <!-- <template #default="scope">
<span class="number" @click="viewDetail(scope.row.id)">{{ scope.row.feeNo }}</span>
</template> -->
            </el-table-column>

            <!-- 工单单号 -->
            <el-table-column label="工单单号" prop="workNo" show-overflow-tooltip width="150">
              <!-- <template #default="scope">
<span class="number" @click="goOrder(scope.row.workId)">{{ scope.row.workNo }}</span>
</template> -->
            </el-table-column>

            <!-- 状态 -->
            <el-table-column label="状态" prop="state" width="83" :align="'center'" fixed="right">
              <template #default="scope">
                <div class="flex_justify_center">
                  <HStatus v-if="scope.row.status === 1" type="warning">待审核</HStatus>
                  <HStatus v-if="scope.row.status === 2" type="success">已通过</HStatus>
                  <HStatus v-if="scope.row.status === 3" type="error">已驳回</HStatus>
                  <HStatus v-if="scope.row.status === 4" type="info">已撒回</HStatus>
                  <HStatus v-if="scope.row.status === 5" type="info">已取消</HStatus>
                </div>
              </template>
            </el-table-column>

            <!-- 结算类型 -->
            <el-table-column
              label="结算类型"
              prop="settlementType"
              show-overflow-tooltip
              width="100"
            />

            <!-- 项目费用名称 -->
            <el-table-column
              label="项目费用名称"
              prop="feeProject"
              show-overflow-tooltip
              min-width="100"
            />

            <!-- 支付网点总费用 -->
            <el-table-column
              label="支付网点总费用"
              prop="networkUndertakePrice"
              show-overflow-tooltip
              width="150"
            >
              <template #default="i">￥{{ i.row.networkUndertakePrice }}</template>
            </el-table-column>

            <!-- 申请人员 -->
            <el-table-column label="申请人员" prop="applyEmp" show-overflow-tooltip width="150" />

            <!-- 申请人员 -->
            <el-table-column label="审核人员" prop="reviewEmp" show-overflow-tooltip width="100" />

            <!-- 申请时间 -->
            <el-table-column label="申请时间" prop="createTime" show-overflow-tooltip width="130" />

            <!-- 审核时间 -->
            <el-table-column label="审核时间" prop="reviewTime" show-overflow-tooltip width="130" />
          </el-table>
        </div>
      </el-tab-pane>

      <!-- 结算明细 -->
      <!-- <el-tab-pane label="结算明细" name="fourth">
        <div class="table">
          <el-table :border="true" :data="tableData" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <el-table-column
              fixed="left"
              label="序号"
              width="50"
              show-overflow-tooltip
              type="index"
              :align="'center'"
            />
            <el-table-column label="申请类型" min-width="200" prop="date" show-overflow-tooltip />
            <el-table-column label="申请费用" prop="city" show-overflow-tooltip width="120" />
            <el-table-column label="审批费用" prop="city" show-overflow-tooltip width="120" />
            <el-table-column label="审核人员" prop="city" show-overflow-tooltip width="120" />
            <el-table-column label="申请时间" prop="city" show-overflow-tooltip width="180" />
            <el-table-column label="更新时间" prop="city" show-overflow-tooltip width="180" />
            <el-table-column label="状态" prop="city" show-overflow-tooltip width="120" />
            <el-table-column fixed="right" label="操作" show-overflow-tooltip width="100">
              <template #default="scope">
                <div class="btns">
                  <button class="btn-table bgColor1" @click="goWork(scope.$index)">工单</button>
                  <router-link class="btn-table bgColor2" to="/order/ocost">查看</router-link>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane> -->

      <!-- 结算价管理 -->
      <!-- <el-tab-pane label="结算价管理" name="fifth">
        <div class="table">
          <el-table :border="true" :data="OrderList" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <el-table-column
              fixed="left"
              label="序号"
              show-overflow-tooltip
              type="index"
              width="50"
              :align="'center'"
            />
            <el-table-column label="模块名称" prop="moduleName" show-overflow-tooltip />
            <el-table-column label="结算费用" prop="price" show-overflow-tooltip>
              <template #default="i">￥{{ i.row.price }}</template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane> -->

      <!-- 账号管理 -->
      <el-tab-pane label="账号管理" name="six">
        <div class="table account_manager">
          <el-table :border="true" :data="networkPersonList" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template>
            <!-- 序号 -->
            <el-table-column
              fixed="left"
              label="序号"
              show-overflow-tooltip
              type="index"
              width="50"
              :align="'center'"
            />

            <!-- 姓名 -->
            <el-table-column label="姓名" prop="userName" show-overflow-tooltip width="100" />

            <!-- 手机号 -->
            <el-table-column
              label="手机号（账号）"
              prop="userPhone"
              show-overflow-tooltip
              width="180"
            />

            <!-- 角色管理 -->
            <el-table-column label="角色管理" prop="superManager" show-overflow-tooltip width="74">
              <template #default="scope">{{
                scope.row.superManager ? '管理员' : '工程师'
              }}</template>
            </el-table-column>

            <!-- 负责人 -->
            <el-table-column align="center" label="负责人" width="70">
              <template #default="scope">
                <div v-if="scope.row.manager" class="manager">
                  <div class="icon">
                    <i class="iconfont icon-dagou"></i>
                  </div>
                  <div class="txt">是</div>
                </div>
                <div v-else class="manager disable">
                  <div class="icon">
                    <i class="iconfont icon-guanbi"></i>
                  </div>
                  <div class="txt">否</div>
                </div>
              </template>
            </el-table-column>

            <!-- 技能 -->
            <el-table-column
              label="技能"
              min-width="200"
              prop="networkEmpSkill"
              show-overflow-tooltip
            />

            <!-- 等级 -->
            <el-table-column
              label="等级"
              prop="networkEmpLevel"
              show-overflow-tooltip
              width="120"
            />

            <!-- 接单策略 -->
            <el-table-column label="接单策略" show-overflow-tooltip width="80">
              <template #default="scope">{{
                scope.row.autoSureReceiveOrder ? '自动确认' : '手动确认'
              }}</template>
            </el-table-column>

            <!-- 人员编号 -->
            <el-table-column label="人员编号" prop="number" show-overflow-tooltip width="100" />

            <!-- 添加时间 -->
            <el-table-column label="添加时间" prop="createTime" show-overflow-tooltip width="128" />

            <!-- 更新时间 -->
            <el-table-column label="更新时间" prop="updateTime" show-overflow-tooltip width="128" />

            <!-- 人员状态 -->
            <el-table-column
              label="人员状态"
              prop="quit"
              show-overflow-tooltip
              width="80"
              fixed="right"
              :align="'center'"
            >
              <template #default="scope">
                <div class="flex_justify_center">
                  <HStatus v-if="scope.row.quit" type="info">禁用</HStatus>
                  <HStatus v-else>启用</HStatus>
                </div>
              </template>
            </el-table-column>

            <!-- 操作 -->
            <el-table-column fixed="right" label="操作" show-overflow-tooltip width="100">
              <template #default="{ row, $index }">
                <el-space :size="0" :spacer="spacer">
                  <h-link @click="handleNetworkPersonAddOrUpdateClick(false, row)">详细</h-link>
                  <h-link
                    v-if="!row.manager"
                    type="error"
                    @click="handleDelPerson(row.empRealtionId)"
                  >
                    删除
                  </h-link>
                  <h-link v-if="$index === 0" @click="handleNetworkPersonAddOrUpdateClick(true)"
                    >添加</h-link
                  >
                </el-space>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>

      <!-- 资质信息 -->
      <!-- <el-tab-pane label="资质信息" name="seven">
        <div class="table" v-loading="getFinancialInfoLoading">
          <el-table :border="true" :data="OrderList" height="100%" resizable stripe>
            <template #empty>
              <el-empty description="暂无数据" />
            </template> -->
      <!-- 申请类型 -->
      <!-- <el-table-column label="申请类型" min-width="200" prop="date" show-overflow-tooltip /> -->

      <!-- 申请费用 -->
      <!-- <el-table-column label="申请费用" prop="city" show-overflow-tooltip width="120" /> -->

      <!-- 审批费用 -->
      <!-- <el-table-column label="审批费用" prop="city" show-overflow-tooltip width="120" /> -->

      <!-- 审核人员 -->
      <!-- <el-table-column label="审核人员" prop="city" show-overflow-tooltip width="120" /> -->

      <!-- 申请时间 -->
      <!-- <el-table-column label="申请时间" prop="city" show-overflow-tooltip width="180" /> -->

      <!-- 更新时间 -->
      <!-- <el-table-column label="更新时间" prop="city" show-overflow-tooltip width="180" /> -->

      <!-- 状态 -->
      <!-- <el-table-column label="状态" prop="city" width="120" /> -->

      <!-- 状态 -->
      <!-- <el-table-column fixed="right" label="操作" width="100">
              <template #default="scope">
                <div class="btns">
                  <button class="btn-table bgColor1" @click="goWork(scope.$index)">工单</button>
                  <router-link class="btn-table bgColor2" to="/order/ocost">查看</router-link>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane> -->

      <!-- 财务信息 -->
      <!-- <el-tab-pane name="financialInfo" label="财务信息">
        <div v-loading="getFinancialInfoLoading" class="financial-info">
          <div class="item">
            <div class="label">银行名称：</div>
            <div class="value">
              <h-tooltip-text
                :text="financialInfo.bandName"
                :max-width="240"
                :height="16"
              ></h-tooltip-text>
            </div>
          </div>
          <div class="item">
            <div class="label">银行卡号：</div>
            <div class="value">
              <h-tooltip-text
                :text="financialInfo.bandNo"
                :max-width="240"
                :height="16"
              ></h-tooltip-text>
            </div>
          </div>
          <div class="item card-holder-name">
            <div class="label">持卡人姓名：</div>
            <div class="value">
              <h-tooltip-text
                :text="financialInfo.cardholderName"
                :max-width="240"
                :height="16"
              ></h-tooltip-text>
            </div>
          </div>
          <div class="item settle-method">
            <div class="label">结算方式：</div>
            <div class="value">{{ financialInfo.settlementType === 1 ? '月结' : '单付' }}</div>
          </div>
          <div class="item">
            <div class="label">营业执照号：</div>
            <div class="value">
              <h-tooltip-text
                :text="financialInfo.businessLicenseNo"
                :max-width="240"
                :height="16"
              ></h-tooltip-text>
            </div>
          </div>
          <div class="item">
            <div class="label">营业执照图片：</div>
            <div v-if="financialInfo.businessLicenseImage" class="value">
              <h-image
                :width="100"
                :height="62.5"
                :src="financialInfo.businessLicenseImage"
                :show-del-btn="false"
              ></h-image>
            </div>
          </div>
        </div>
      </el-tab-pane> -->

      <!-- 网点权限策略 -->
      <el-tab-pane label="网点权限策略" name="networkPermissionStrategy">
        <div v-loading="getStrategyLoading" class="network-permission-strategy">
          <template v-if="strategyDetail">
            <div class="permission">
              <div class="label">接单策略：</div>
              <div class="value">{{
                strategyDetail.autoSureReceiveOrder ? '自动接单' : '手动接单'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">取消工单：</div>
              <div class="value">{{
                strategyDetail.autoCheckCancelOrder ? '自动审核' : '人工审核'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">结单审核：</div>
              <div class="value">{{
                strategyDetail.autoCheckEndOrder ? '自动审核' : '人工审核'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">物料价格：</div>
              <div class="value">{{
                strategyDetail.enableUpdateSparePrice ? '允许修改' : '禁止修改'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">工单费用查看：</div>
              <div class="value">{{ strategyDetail.allowSelectFee ? '允许查看' : '禁止查看' }}</div>
            </div>
            <div class="permission">
              <div class="label">费用查看权限：</div>
              <div class="value">
                <h-tooltip-text
                  :text="
                    strategyDetail.selectFeeEmpRoots
                      ? strategyDetail.selectFeeEmpRoots.join('、')
                      : ''
                  "
                  :height="16"
                  :max-width="100"
                ></h-tooltip-text>
              </div>
            </div>
            <div class="permission">
              <div class="label">结算权限：</div>
              <div class="value">{{
                strategyDetail.allowWithdrawal ? '允许结算' : '禁止结算'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">结算人员：</div>
              <div class="value">
                <h-tooltip-text
                  :text="
                    strategyDetail.withdrawEmpRoots
                      ? strategyDetail.withdrawEmpRoots.join('、')
                      : ''
                  "
                  :height="16"
                  :max-width="100"
                ></h-tooltip-text>
              </div>
            </div>
            <div class="permission">
              <div class="label">结算手续费策略：</div>
              <div class="value">{{
                strategyDetail.withdrawHandlingFeeStrategy === 1 ? '按比例收取' : '每单固定'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">手续费比例(%)：</div>
              <div class="value">{{ (strategyDetail.handingFeeProportion || 0).toFixed(2) }}</div>
            </div>
            <div class="permission">
              <div class="label">每单固定手续费：</div>
              <div class="value">￥{{ (strategyDetail.handingFeeFixedOrder || 0).toFixed(2) }}</div>
            </div>
            <div class="permission">
              <div class="label">防伪查询：</div>
              <div class="value">{{ strategyDetail.allowSecurityCheck ? '开启' : '关闭' }}</div>
            </div>
            <div class="permission">
              <div class="label">新建工单：</div>
              <div class="value">{{ strategyDetail.allowCreateOrder ? '开启' : '关闭' }}</div>
            </div>
            <div class="permission">
              <div class="label">物料申请：</div>
              <div class="value">{{ strategyDetail.allowApplySpare ? '开启' : '关闭' }}</div>
            </div>
            <div class="permission">
              <div class="label">知识库查看：</div>
              <div class="value">{{ strategyDetail.allowApplySpare ? '开启' : '关闭' }}</div>
            </div>
            <div class="permission">
              <div class="label">结算规则：</div>
              <div class="value">{{
                strategyDetail.allowManagerHelpWithdraw ? '允许管理员代结算' : '禁止管理员代结算'
              }}</div>
            </div>
            <div class="permission">
              <div class="label">免费额度查看：</div>
              <div class="value">{{
                strategyDetail.allowSelectAmount ? '允许查看' : '禁止查看'
              }}</div>
            </div>
          </template>
        </div>
      </el-tab-pane>

      <!-- 网点额度管理 -->
      <el-tab-pane v-loading="getAmountLimitLoading" label="网点额度管理" name="networkSaleAmount">
        <div v-loading="getAmountLimitLoading" class="amount-limit">
          <div class="amount-limit-item">
            <div class="label">经销商销售金额:</div>
            <div class="value">￥{{ (networkAmountLimit.saleAmount || 0).toFixed(2) }}</div>
          </div>
          <div class="amount-limit-item">
            <div class="label">免费额度比例:</div>
            <div class="value">{{ networkAmountLimit.freeAmountPercentage }}%</div>
          </div>
          <div class="amount-limit-item">
            <div class="label">年度总额度:</div>
            <div class="value">￥{{ (networkAmountLimit.annualTotalAmount || 0).toFixed(2) }}</div>
          </div>
          <div class="amount-limit-item">
            <div class="label">可用额度:</div>
            <div class="value">￥{{ (networkAmountLimit.freeAmount || 0).toFixed(2) }}</div>
          </div>
          <div class="amount-limit-item">
            <div class="label">冻结金额:</div>
            <div class="value">￥{{ networkAmountLimit.freezeAmount.toFixed(2) }}</div>
          </div>
          <div class="amount-limit-item">
            <div class="label">已发放金额:</div>
            <div class="value">￥{{ networkAmountLimit.grantAmount.toFixed(2) }}</div>
            <h-button
              :height="20"
              :padding-lr="6"
              type="primary"
              @click="updateGrantAmountModalVisible = true"
            >
              修改
            </h-button>
          </div>
          <div class="amount-limit-item">
            <div class="label">去年剩余可用额度:</div>
            <div class="value">￥{{ networkAmountLimit.broughtForwardAmount.toFixed(2) }}</div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

    <!-- 分页 -->
    <div
      v-if="
        !['seven', 'networkPermissionStrategy', 'financialInfo', 'networkSaleAmount'].includes(
          activeName,
        )
      "
      class="footer"
    >
      <Pagination
        ref="pageRef"
        :list_total="total"
        @size-change="getnetworkListRows()"
        @current-change="getnetworkListRows()"
      />
    </div>
    <SelfDefined
      v-model="addDialog"
      :get-row="currentOperateNetworkPerson"
      :is-add="isAddNetworkPerson"
      :network-id="(route.params.id as string)"
      :team-type="teamType"
      @update-list="getnetworkListRows"
    />

    <update-grant-amount-modal
      v-model="updateGrantAmountModalVisible"
      :network-id="(route.params.id as string)"
      @update="getNetworkAmount(route.params.id as string)"
    />
  </div>
</template>

<script lang="ts" setup>
  import Pagination from '@/components/Pagination/index.vue'
  import { nextTick, onMounted, reactive, ref, watch, toRefs, h } from 'vue'
  import { ElDivider, ElMessage, ElMessageBox } from 'element-plus'
  import { onBeforeRouteLeave, useRoute, useRouter } from 'vue-router'
  import SelfDefined from '../../../network/emp/components/SelfDefined.vue'
  import {
    deleteNetworkEmp,
    getNetworkDetailAuthenField,
    getNetworkDetailEmpList,
    getNetworkDetailModuleSettlementList,
    getNetworkDetailOrderFeeList,
    getNetworkDetailOrderList,
    getNetworkDetailOrderSpareList,
  } from '@/api/network'
  import { useGetStrategy, useGetFinancial } from '@/services/pages/network/detail'
  import { useNetworkAmountLimitService } from '@/services/module/network/useNetworkAmountLimitService'
  import UpdateGrantAmountModal from './components/update-grant-amount-modal.vue'
  import { HMessageBox } from '@/components/common/message'

  const { networkAmountLimit, getAmountLimitLoading, getNetworkAmount } =
    useNetworkAmountLimitService()
  const { strategyDetail, getStrategyLoading, getStrategy } = useGetStrategy()
  const { financialInfo, getFinancialInfoLoading, getFinancialInfo } = useGetFinancial()

  const props = defineProps({
    teamType: [String, Number],
  })
  const { teamType } = toRefs(props)

  const updateGrantAmountModalVisible = ref(false)

  // 间距组件的间隔符
  const spacer = h(ElDivider, {
    direction: 'vertical',
    style: 'display: flex; align-items: center;',
  })

  const handleDelPerson = (id: number) => {
    ElMessageBox.confirm('是否删除网点人员', '提示', {
      cancelButtonText: '取消',
      confirmButtonText: '确认',
      type: 'error',
      autofocus: false,
    })
      .then(() => {
        deleteNetworkEmp(id).then((res) => {
          if (res.data.success) {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'success',
              duration: 5000,
              plain: true,
            })
            NetworkDetailEmpList()
          } else {
            ElMessage({
              showClose: true,
              message: res.data.message,
              type: 'error',
              duration: 5000,
              plain: true,
            })
          }
        })
      })
      .catch(() => {
        //
      })
  }

  const router = useRouter()
  const route = useRoute()
  const pageRef = ref()
  const activeName = ref('first')
  const addDialog = ref(false)
  let total = ref(0)
  const isAddNetworkPerson = ref(false)

  let OrderList = ref(null)
  let networkPersonList = ref<Array<NetworkAPIResponse.NetworkPersonListItem>>([])
  let data = reactive({
    limit: 50,
    page: 1,
  })
  const emits = defineEmits(['loading-change'])
  const tableData = []
  // 查看工单
  const goWork = (n: number) => {
    router.push('/order/odetail/' + n)
  }

  watch(activeName, (n) => {
    total.value = 0
    switch (n) {
      case 'first':
        NetworkDetailOrderList(getNetworkDetailOrderList)
        break
      case 'second':
        NetworkDetailOrderList(getNetworkDetailOrderSpareList)
        break
      case 'third':
        NetworkDetailOrderList(getNetworkDetailOrderFeeList)
        break
      case 'fourth':
        break
      case 'fifth':
        NetworkDetailOrderList(getNetworkDetailModuleSettlementList)
        break
      case 'six':
        nextTick(() => {
          NetworkDetailEmpList()
        })
        break
      case 'seven':
        // getNetworkDetailAuthenFielddata()
        break
      case 'networkSaleAmount':
        getNetworkAmount(route.params.id as string)
        break
    }
  })

  // 点击分页
  const getnetworkListRows = () => {
    switch (activeName.value) {
      case 'first':
        NetworkDetailOrderList(getNetworkDetailOrderList)
        break
      case 'second':
        NetworkDetailOrderList(getNetworkDetailOrderSpareList)
        break
      case 'third':
        NetworkDetailOrderList(getNetworkDetailOrderFeeList)
        break
      case 'fourth':
        break
      case 'fifth':
        NetworkDetailOrderList(getNetworkDetailModuleSettlementList)
        break
      case 'six':
        nextTick(() => {
          NetworkDetailEmpList()
        })
        break
      case 'seven':
        // getNetworkDetailAuthenFielddata()
        break
    }
  }
  const getNetworkDetailAuthenFielddata = async () => {
    OrderList.value = null

    emits('loading-change', true)
    await getNetworkDetailAuthenField(route.params.id).then((res) => {
      if (res.data.success) {
        OrderList.value = res.data.data.rows
      } else {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'error',
          duration: 5000,
          plain: true,
        })
      }
      emits('loading-change', false)
    })
  }
  onMounted(async () => {
    await NetworkDetailOrderList(getNetworkDetailOrderList)
  })

  // 工单明细列表or工单物料
  async function NetworkDetailOrderList(port) {
    OrderList.value = null
    data.limit = pageRef.value?.size
    data.page = pageRef.value?.current
    emits('loading-change', true)
    await port(data, route.params.id).then((res) => {
      if (res.data.success) {
        OrderList.value = res.data.data.rows
        total.value = Number(res.data.data.total)
      } else {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'error',
          duration: 5000,
          plain: true,
        })
      }
      emits('loading-change', false)
    })
  }

  // 获取员工管理
  async function NetworkDetailEmpList() {
    data.limit = pageRef.value?.size
    data.page = pageRef.value?.current
    emits('loading-change', true)
    getNetworkDetailEmpList(data, route.params.id as string)
      .then((res) => {
        if (res.data.code === 20000) {
          networkPersonList.value = res.data.data.rows
          total.value = Number(res.data.data.total)
        } else {
          HMessageBox.error(res.data.message)
        }
        emits('loading-change', false)
      })
      .catch(() => {
        HMessageBox.error('获取员工列表失败，请稍后重试')
      })
  }

  // 添加or修改员工信息
  const currentOperateNetworkPerson = ref<NetworkAPIResponse.NetworkPersonListItem | undefined>(
    undefined,
  )

  function handleNetworkPersonAddOrUpdateClick(
    isAdd: boolean,
    row?: NetworkAPIResponse.NetworkPersonListItem,
  ) {
    addDialog.value = true
    isAddNetworkPerson.value = isAdd
    if (row) {
      currentOperateNetworkPerson.value = row
    }
  }

  const onTabChange = () => {
    if (activeName.value === 'networkPermissionStrategy') {
      getStrategy(route.params.id as string)
    }

    if (activeName.value === 'financialInfo') {
      getFinancialInfo(route.params.id as string)
    }
  }

  onBeforeRouteLeave(() => {
    addDialog.value = false
  })
</script>
<style lang="scss" scoped>
  @import './TablePane.scss';

  .amount-limit {
    display: flex;
    column-gap: 36px;
    row-gap: 8px;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 0 24px;
    flex-wrap: wrap;

    .amount-limit-item {
      display: flex;
      font-size: 16px;
      column-gap: 4px;

      .label {
        color: #606266;
        white-space: nowrap;
      }

      .value {
        font-weight: bold;
        color: #007cff;
        white-space: nowrap;
      }
    }
  }
</style>
